const AdminBiz = require('../../../../store/admin_biz_config.js');
// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper } = pathImport;;
// 引入MobX绑定
import { createStoreBindings } from '../../../../../../miniprogram_npm/mobx-miniprogram-bindings/index';
import { adminStore } from '../../../../config/store_config.js';

Page({

	/**
	 * 页面的初始数据
	 */
	data: {},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: adminStore,
			fields: ['statistics'],
			actions: ['loadHomeData', 'clearVouch', 'adminLogout', 'isAdmin', 'getAdminToken']
		});
		
		if (!this.isAdmin(this)) return;

		await this.loadHomeData();
		
		let admin = this.getAdminToken();
		this.setData({
			isLoad: true,
			admin
		});
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		await this.loadHomeData();
		wx.stopPullDownRefresh();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.loadHomeData();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		// 清理MobX绑定
		this.storeBindings.destroyStoreBindings();
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	bindMoreTap: function (e) {
		let itemList = ['取消所有首页推荐'];
		wx.showActionSheet({
			itemList,
			success: async res => {
				let idx = res.tapIndex;

				if (idx == 0) {
					this._clearVouch();
				}

			},
			fail: function (res) { }
		})
	},

	_clearVouch: async function (e) {
		let cb = async () => {
			const result = await this.clearVouch();
			if(result) {
				pageHelper.showSuccToast('操作成功');
			}
		};
		pageHelper.showConfirm('您确认清除所有首页推荐？', cb)
	},

	bindExitTap: function (e) {
		let callback = () => {
			this.adminLogout();
			wx.reLaunch({
				url: pageHelper.fmtURLByPID('/pages/my/index/my_index'),
			});
		}
		pageHelper.showConfirm('您确认退出?', callback);
	},

})